module.exports = function (grunt) {	
	var taskArray = ['concat:bui','concat:editor','jshint','concat:all','uglify:buildall','copy:main','cssmin','copy:web','watch'];
	//var taskArray = ['concat:bui','concat:editor','jshint','concat:all','copy:main','cssmin','watch'];
	grunt.initConfig({
		pkg: grunt.file.readJSON('package.json'),
		meta: {
			banner: '/*! <%= pkg.name %>-v<%= pkg.version %> - ' +
					'<%= grunt.template.today("yyyy-mm-dd HH:mi:ss") %> \nCopyright (c): kevin.huang <%= pkg.site %> \nReleased under MIT License*/'
		},
		jshint: {
			options: {
				jshintrc: '.jshintrc'
			},
			build: ['build/javascript/edit-<%= pkg.version %>.js']
		},
		copy: {	
			main:{
				files: [				
					{  expand: true, cwd: 'src/theme/', src: ['**'], dest: 'build/theme/' },
					{  src: ['src/javascript/editor/editConfig.js'], dest: 'build/javascript/editConfig.js' },
					{  src: ['src/javascript/editor/emotionConfig.js'], dest: 'build/javascript/emotionConfig.js' }
				]
			},
			web:{
				//复制到ssm项目
				files: [				
					{
						src: ['build/javascript/editConfig.js'],
						dest: 'E:/myprojects/bui-ssm/web/src/main/webapp/static/lib/editConfig.js'
					},
					{
						src: ['build/javascript/emotionConfig.js'],
						dest: 'E:/myprojects/bui-ssm/web/src/main/webapp/static/lib/emotionConfig.js'
					}
					, {
						src: ['build/<%= pkg.toweb %>/edit-<%= pkg.version %>.js'],
						dest: 'E:/myprojects/bui-ssm/web/src/main/webapp/static/lib/edit-<%= pkg.version %>.js'
					}, 
					
					{//复制到 editor 项目
						expand: true,
						cwd: 'build/',
						src: ['**'],
						dest: 'E:/myprojects/bui-ssm/editor/src/main/webapp/'
					}
				]
			}
		},
		uglify: {
			options: {
				mangle: true, //不混淆变量名
				stripBanners: true,
				banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
					'<%= grunt.template.today("yyyy-mm-dd") %> \nCopyright (c): kevin.huang <%= pkg.site %> \nReleased under MIT License*/\n'
			},
			buildall: {//压缩
				options: {
                    mangle: true, //不混淆变量名
                    preserveComments: 'false'//不删除注释，还可以为 false（删除全部注释），some（保留@preserve @license @cc_on等注释）                    
				},
				files: [{
					expand: true,
					cwd: 'build/javascript',//js目录下
					src: '**/*.js',//所有js文件
					dest: 'build/javascript-min'//输出到此目录下
				}]
			}
		},
		cssmin: {
			options: {
				mergeIntoShorthands: false,
				roundingPrecision: -1
			},
			target: {
				files: {
					'build/theme/font/bui-fonts.min.css': ['build/theme/font/bui-fonts.css'],
					'build/theme/blue/main.min.css': ['build/theme/blue/main.css']
				}
			}
		},
		concat: {
			options: {
				//封装为一个文件
				process: function(src, filepath) {
					console.log(filepath);					
					if(filepath.indexOf("header") > 0){						
						var newsrc = 
							"(function (global, factory) {\n"+
							"	if (typeof define === 'function' && define.amd  && !window['_all_in_']) {\n"+
							"		define(['$B', 'toolbar', 'panel', 'colorpicker', 'plugin', 'utils', 'resize', 'mutilUpload', 'editConfig'], function (_$B) {\n"+
							"			return factory(global, _$B);\n"+
							"		});\n"+
							"	} else {\n"+
							"		if (!global['$B']) {\n"+
							"			global['$B'] = {};\n"+
							"		}\n"+
							"		factory(global, global['$B']);\n"+
							"	}\n"+
							"}(typeof window !== 'undefined' ? window : this, function (window, $B) {\n" + src;
						
						return newsrc;
					}
					if(filepath.indexOf("undo") > 0){
						return src + '\n   $B["Edit"] = Kedit;'
									+'\n}));';
					}
					return src;
				},
				separator: '\n',
				stripBanners: true,
				banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
					'<%= grunt.template.today("yyyy-mm-dd hh:mm:ss") %> \nCopyright (c): kevin.huang  <%= pkg.site %> \nReleased under MIT License*/\n'
				
			},			
			bui: {
				src: [	
						'src/javascript/bui/config.js',
						'src/javascript/bui/basic.js',
						'src/javascript/bui/colorpicker.js',
						'src/javascript/bui/panel.js',
						'src/javascript/bui/plugin.js',
						'src/javascript/bui/resize.js',
						'src/javascript/bui/toolbar.js',
						'src/javascript/bui/upload.js',
						'src/javascript/bui/utils.js'],
				dest:   'build/javascript/edit-deps-<%= pkg.version %>.js'
			},
			editor:{
				src:[
					'src/javascript/editor/header.js',
					'src/javascript/editor/toolbar.js',
					'src/javascript/editor/core.js',
					'src/javascript/editor/functions.js',
					'src/javascript/editor/code.js',
					'src/javascript/editor/float.js',
					'src/javascript/editor/stack.js',
					'src/javascript/editor/table.js',
					'src/javascript/editor/undo.js'
				],
				dest:   'build/javascript/edit-<%= pkg.version %>.js'
			},
			all:{
				src: ['build/javascript/edit-deps-<%= pkg.version %>.js',
						'build/javascript/edit-<%= pkg.version %>.js'],
				dest: 'build/javascript/edit-all-<%= pkg.version %>.js'
			}
		},
		watch: {
			build: {
				files: ['src/javascript/bui/*.js', 
						'src/javascript/editor/*.js',
						'src/theme/default/*.css'],
				tasks: taskArray,
				options: { spawn: false }
			}
		}
	});
	grunt.loadNpmTasks('grunt-contrib-copy');
	grunt.loadNpmTasks('grunt-contrib-concat');
	grunt.loadNpmTasks('grunt-contrib-watch');
	grunt.loadNpmTasks('grunt-contrib-jshint');
	grunt.loadNpmTasks('grunt-contrib-cssmin');
	grunt.loadNpmTasks('grunt-contrib-uglify');
	grunt.registerTask('default', taskArray);
}